//minetrig.txt - This is the trigger to a set of nearby mines. When the PC gets close to it,
//  it will activate all fo those mines. This can't be disarmed, but the mines nearby can.
// The mines are set off by sending message 105. Sets off mines of script chainmine.
//Cell 0 - If 1, doesn't ever go off
//Cell 1,2 - A stuff done flag. If > 0, this mine has been deactivated or was set off
//Cell 3 - This is the number of the first of the mines it sets off.
//Cell 4 - This is the number of the last of the mines it sets off.


beginobjectscript; // mine

variables;
short near_char;
short r1;
short icon_off = 0;
short i;
short warning_used = 0;
short wisp_counter = 0;
short cur_tick;

body;

beginstate INIT_STATE;
	cur_tick = get_current_tick();
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 
	// turn mine off if flag set
	if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) {
		if (get_sdf(get_memory_cell(1),get_memory_cell(2)) > 0) {
			if (icon_off == 0)
				set_object_icon(ME,4);
			icon_off = 1;
			end();
			}
		}
	
	// at this point, if mine is off, turn it back on.
	if (icon_off == 1) {
		icon_off = 0;
		set_object_icon(ME,0);
		}
	
	// fire wisps	
	wisp_counter = wisp_counter + 1;
	if (wisp_counter > 1)
		wisp_counter = 0;
	if (wisp_counter == 1) {
		r1 = get_ran(1,get_memory_cell(3),get_memory_cell(4));
		shoot_projectile(ME,1000 + r1,34);
		}
	near_char = -1;	
	if (dist_to_pc() <= 2) {
		if (can_detect_pc(5) > 0)
			near_char = pc_num();
		}
	if ((near_char >= 0) && (get_memory_cell(0) == 0)) {
		pc_heard_sound(117);
		run_sparkles_on_object(ME,172,1,4);
		
		i = get_memory_cell(3);
		while (i <= get_memory_cell(4)) {
			give_object_message(i,105);
			i = i + 1;
			} 
			
		if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) 
			sf(get_memory_cell(1),get_memory_cell(2),1);
			
		kill_object(ME,0);
		}
		else if ((dist_to_pc() <= 5) && (get_memory_cell(0) == 0)) {
			if ((get_nearest_party_char(5) >= 0) && (warning_used == 0)) {
				create_text_bubble("Sssss ...");
				print_str("The mine trigger begins to hiss when you get close.");
				pc_heard_sound(237);
				warning_used = 1;
				}
			}
			else if (dist_to_pc() > 5) {
				if ((dist_to_pc() <= 15) && (cur_tick != get_current_tick())) {
					cur_tick = get_current_tick();
					create_missile_spiral(158,6,2,2);
					}
				warning_used = 0;	
				}
break;

beginstate 3;
	
break;